package com.nd.controller.admin;

import com.nd.Utils.ValidUtil;
import com.nd.pojo.bean.Issue;
import com.nd.pojo.bean.Keyword;
import com.nd.pojo.bo.*;
import com.nd.pojo.vo.*;
import com.nd.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 商场管理
 *                      行政区域 region
 *                      品牌制造商 brang
 *                      商品类目 category
 *                      订单管理 order
 *                      通用问题 issue
 *                      关键词 keyword
 * @author: Aaron
 * @create: 2021-06-04 20:03
 **/
@RestController
@RequestMapping("admin")
public class MallManageController {

    @Autowired
    RegionService regionService;
    @Autowired
    BrandService brandService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    OrderService orderService;
    @Autowired
    IssueService issueService;
    @Autowired
    KeywordService keywordService;

    @RequestMapping("region/list")
    public BaseRespVo regionList() {
        List<RegionVO> regionVOList = regionService.regionList();
        return BaseRespVo.ok(regionVOList);
    }

    @RequestMapping("brand/list")
    public BaseRespVo brangList(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        BrandListVO brandListVO = brandService.brangList(page,limit,id,name,sort,order);
        return BaseRespVo.ok(brandListVO);
    }

    @RequestMapping("brand/create")
    public BaseRespVo brandCreate(@RequestBody BrandCreateBO brandCreateBO) {
        try {
            if ("".equals(brandCreateBO.getDesc()) || new BigDecimal(brandCreateBO.getFloorPrice()) == null || "".equals(brandCreateBO.getName()) || "".equals(brandCreateBO.getPicUrl()) || brandCreateBO.getPicUrl() == null) {
                return BaseRespVo.fail("参数不正确");
            }
        } catch (Exception e) {
            return BaseRespVo.fail("参数不正确");
        }
        if (new BigDecimal(brandCreateBO.getFloorPrice()).compareTo(new BigDecimal(0)) != 1) {
            return BaseRespVo.fail("参数不正确");
        }
        BrandCreateVO brandCreateVO = brandService.brandCreate(brandCreateBO);
        return BaseRespVo.ok(brandCreateVO);
    }

    @RequestMapping("brand/update")
    public BaseRespVo brandUpdate(@RequestBody BrandUpdateBO brandUpdateBO) {
        try {
            if ("".equals(brandUpdateBO.getDesc()) || new BigDecimal(brandUpdateBO.getFloorPrice()) == null || "".equals(brandUpdateBO.getName()) || "".equals(brandUpdateBO.getPicUrl()) || brandUpdateBO.getPicUrl() == null) {
                return BaseRespVo.fail("参数不正确");
            }
        } catch (Exception e) {
            return BaseRespVo.fail("参数不正确");
        }
        if (new BigDecimal(brandUpdateBO.getFloorPrice()).compareTo(new BigDecimal(0)) != 1) {
            return BaseRespVo.fail("参数不正确");
        }
        BrandUpdateVO brandUpdateVO = brandService.brandUpdate(brandUpdateBO);
        return BaseRespVo.ok(brandUpdateVO);
    }

    @RequestMapping("brand/delete")
    public BaseRespVo brandDelete(@RequestBody BrandUpdateBO brandUpdateBO) {
        brandService.brandDelete(brandUpdateBO.getId());
        return BaseRespVo.ok();
    }

    @RequestMapping("category/l1")
    public BaseRespVo categoryL1() {
        List<CateGoryL1VO> cateGoryL1VOList = categoryService.categoryL1();
        return BaseRespVo.ok(cateGoryL1VOList);
    }

    @RequestMapping("category/list")
    public BaseRespVo categoryList() {
        List<CategoryListAllVO> categoryListAllVOS = categoryService.categoryList();
        return BaseRespVo.ok(categoryListAllVOS);
    }

    @RequestMapping("category/create")
    public BaseRespVo categoryCreate(@RequestBody CategoryCreateBO categoryCreateBO) {
        if ("".equals(categoryCreateBO.getName()) || categoryCreateBO.getName() == null
            || "".equals(categoryCreateBO.getKeywords()) || categoryCreateBO.getKeywords() == null
            || "".equals(categoryCreateBO.getLevel()) || categoryCreateBO.getLevel() == null
            || "".equals(categoryCreateBO.getPid()) || categoryCreateBO.getPid() == null
            || "".equals(categoryCreateBO.getDesc()) || categoryCreateBO.getDesc() == null
            || "".equals(categoryCreateBO.getIconUrl()) || categoryCreateBO.getIconUrl() == null
            || "".equals(categoryCreateBO.getPicUrl()) || categoryCreateBO.getPicUrl() == null
        ) {
            return BaseRespVo.fail("把参数写全咯");
        }

        if ("L2".equals(categoryCreateBO.getLevel()) && categoryCreateBO.getPid() == 0) {
            return BaseRespVo.fail("记得选择父类");
        }
        CategoryCreateVO categoryCreateVO = categoryService.categoryCreate(categoryCreateBO);
        return BaseRespVo.ok(categoryCreateVO);
    }


    @RequestMapping("category/update")
    public BaseRespVo categoryUpdate(@RequestBody CategoryUpdateBO categoryUpdateBO) {
        try {
            int cont = categoryService.categoryUpdate(categoryUpdateBO);
            if (cont == 1) {
                return BaseRespVo.ok();
            } else if (cont == 2) {
                return BaseRespVo.fail("禁止一级类转成自己的二级类 ");
            } else {
                return BaseRespVo.fail("一级类目转成二级类目的前提是一级类目下无二级类目");
            }
        } catch (Exception e) {
            return BaseRespVo.fail("参数错误");
        }
    }

    @RequestMapping("category/delete")
    public BaseRespVo categoryDelete(@RequestBody CategoryDeleteBO categoryDeleteBO) {
        categoryService.categoryDelete(categoryDeleteBO);
        return BaseRespVo.ok();
    }


    @RequestMapping("order/list")
    public BaseRespVo orderList(Integer page, Integer limit, Short orderStatusArray, String sort, String order,String orderSn,Integer userId) {

        OrderListVO orderListVO = orderService.orderList(page,limit,orderStatusArray,sort,order,orderSn,userId);
        return BaseRespVo.ok(orderListVO);
    }

    @RequestMapping("order/detail")
    public BaseRespVo orderDetail(Integer id) {
        OrderDetailVO orderDetailVO = orderService.orderDetail(id);
        return BaseRespVo.ok(orderDetailVO);
    }

    @RequestMapping("order/refund")
    public BaseRespVo orderRefund(@RequestBody OrderRefundBO orderRefundBO) {
        orderService.orderRefund(orderRefundBO);
        return BaseRespVo.ok();
    }

    @RequestMapping("order/ship")
    public BaseRespVo orderShip(@RequestBody OrderShipBO orderShipBO) {
        orderService.orderShip(orderShipBO);
        return BaseRespVo.ok();
    }

    @RequestMapping("issue/list")
    public BaseRespVo issueList(Integer page, Integer limit, String question, String sort, String order) {
        IssueListVO issueListVO = issueService.issueList(page,limit,question,sort,order);
        return BaseRespVo.ok(issueListVO);
    }

    @RequestMapping("issue/create")
    public BaseRespVo issueCreate(@RequestBody IssueCreateBO issueCreateBO) {
        IssueCreateVO issueCreateVO = issueService.issueCreate(issueCreateBO);
        return BaseRespVo.ok(issueCreateVO);
    }

    @RequestMapping("issue/update")
    public BaseRespVo issueUpdate(@RequestBody Issue issue) {
        Issue issueVO = issueService.issueUpdate(issue);
        return BaseRespVo.ok(issueVO);
    }
    @RequestMapping("issue/delete")
    public BaseRespVo issueDelete(@RequestBody Issue issue) {
        issueService.issueDelete(issue);
        return BaseRespVo.ok();
    }

    @RequestMapping("keyword/list")
    public BaseRespVo keywordList(Integer page, Integer limit, String keyword, String url, String sort, String order) {
        KeywordListVO keywordListVO = keywordService.keywordList(page,limit,keyword,url,sort,order);
        return BaseRespVo.ok(keywordListVO);
    }

    @RequestMapping("keyword/create")
    public BaseRespVo keywordCreate(@RequestBody KeywordCreateBO keywordCreateBO) {
        if ("".equals(keywordCreateBO.getKeyword()) || keywordCreateBO.getKeyword() == null || "".equals(keywordCreateBO.getUrl()) || keywordCreateBO.getUrl() == null || keywordCreateBO.getIsHot() == null || keywordCreateBO.getIsDefault() == null) {
            return BaseRespVo.fail("把参数写全咯");
        }
        KeywordCreateVO keywordCreateVO = keywordService.keywordCreate(keywordCreateBO);
        return BaseRespVo.ok(keywordCreateVO);
    }

    @RequestMapping("keyword/update")
    public BaseRespVo keywordUpdate(@RequestBody Keyword keyword) {
        if ("".equals(keyword.getKeyword()) || keyword.getKeyword() == null || "".equals(keyword.getUrl()) || keyword.getUrl() == null || keyword.getIsHot() == null || keyword.getIsDefault() == null) {
            return BaseRespVo.fail("把参数写全咯");
        }
        Keyword keywordVO = keywordService.keywordUpdate(keyword);
        return BaseRespVo.ok(keywordVO);
    }

    @RequestMapping("keyword/delete")
    public BaseRespVo keywordDelete(@RequestBody Keyword keyword) {
        keywordService.keywordDelete(keyword);
        return BaseRespVo.ok();
    }

}
